package reduce.fz.com.reduce.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.framewrok.log.FeelLog;

/**
 */
public class JsonUtil {
    /**
     * 从CLASS映射对象
     *
     * @param res
     * @param cls
     * @param <T>
     * @return
     */
    public static <T> T convert(String res, Class<T> cls) {
        return JSON.parseObject(res, cls);
    }


    public static <T> T convert(String res, TypeReference<T> type) {
        return JSON.parseObject(res, type.getType());
    }

    public static <T> T convertData(String res, Class<T> cls) {
        try {
            Map<String, Object> res2 = JSON.parseObject(res, new TypeReference<Map<String, Object>>() {
            }.getType());
            Object r = res2.get("data");
            String tmpJson = JSON.toJSONString(r);
            return JSON.parseObject(tmpJson, cls);
        } catch (Exception e) {
            FeelLog.e(e);
            return null;
        }
    }

    public static <T> T convertClass(String res, Class<T> cls) {
        try {
            Result res2 = JSON.parseObject(res, Result.class);
            if (!res2.isSuccess())
                throw new RuntimeException();
            String tmpJson = JSON.toJSONString(res2.getData());
            return JSON.parseObject(tmpJson, cls);
        } catch (Exception e) {
            FeelLog.e(e);
            return null;
        }
    }

    public static <T> T convertObj(String res, Class<T> cls) {
        try {
            Result res2 = JSON.parseObject(res, Result.class);
            if (!res2.isSuccess())
                throw new RuntimeException();
            String tmpJson = JSON.toJSONString(res2.getData());
            return JSON.parseObject(tmpJson, cls);
        } catch (Exception e) {
            FeelLog.e(e);
            return null;
        }
    }

    public static <T> List<T> convertList(String res, Class<T> cls) {
        try {
            Result res2 = JSON.parseObject(res, Result.class);
            if (!res2.isSuccess())
                throw new RuntimeException();
            String tmpJson = JSON.toJSONString(res2.getData());
            return JSON.parseArray(tmpJson, cls);
        } catch (Exception e) {
            FeelLog.e(e);
            return null;
        }
    }

    public static <T> T converObjFromMap(Map<String, Object> map, String dataName, Class<T> cls) {
        Object o = map.get(dataName);
        String tmpJson = JSON.toJSONString(o);
        return JSON.parseObject(tmpJson, cls);
    }

    public static <T> List<T> converListFromMap(Map<String, Object> map, String dataName, Class<T> cls) {
        Object o = map.get(dataName);
        String tmpJson = JSON.toJSONString(o);
        return JSON.parseArray(tmpJson, cls);
    }


    public static String convertToString(Object data) {
        try {
            return JSON.toJSONString(data);
        } catch (Exception e) {
            FeelLog.e(e);
            return null;
        }
    }


    public static <T> T convertWithData(String res, Class<T> cls, String dataName) {
        try {
            Map<String, Object> res2 = JSON.parseObject(res, new TypeReference<Map<String, Object>>() {
            }.getType());
            Map<String, Object> r = (Map<String, Object>) res2.get("data");
            Object o = r.get(dataName);
            String tmpJson = JSON.toJSONString(o);
            return JSON.parseObject(tmpJson, cls);
        } catch (Exception e) {
            FeelLog.e(e);
            return null;
        }
    }


    public static <T> T fromJson(String json, java.lang.reflect.Type typeOfT) {
        try {
            return JSON.parseObject(json, typeOfT);
        } catch (Throwable e) {
            FeelLog.e(new Exception(json, e));
            return null;
        }
    }


}
